<?php
class MSEP_Upload extends MSEP_Object
{


      private $mb;



      private $exts;



      private $inputs;



      private $user_id;



      private $album;



      private $path;



      private $prefix;



      public function __init(MSEP $core)
      {
            $this->core = $core;

            $this->mb = 10;

            $this->exts = "*.*";

            $this->inputs = "";

            $this->user_id = 0;

            $this->album = 0;

            $this->path = "";

            $this->prefix = "";
      }



      public function init($mb, $exts, $inputs, $user_id, $album, $path = "", $is_admin = true)
      {
            if (!empty($mb) && is_numeric($mb))
            {
                  $this->mb = $mb;
            }

            if (!empty($exts))
            {
                  $this->exts = "";

                  foreach ($exts as $ext)
                  {
                        $this->exts .= "*.{$ext};";
                  }
            }

            if (!empty($inputs))
            {
                  foreach ($inputs as $input)
                  {
                        $this->inputs .= "\"{$input}\",";
                  }
            }

            if (!empty($user_id) && $this->core->_('database')->num_rows($this->core->_('database')->query("SELECT user_id FROM se_users WHERE user_id = '{$user_id}'")) > 0)
            {
                  $this->user_id = $user_id;
            }

            if (!empty($album) && is_numeric($album))
            {
                  $this->album = $album;
            }

            if (!empty($path))
            {
                  $this->path = $path;
            }

            if ($is_admin)
            {
                  $this->prefix = "../";
            }
            else
            {
                  $this->prefix = "";
            }
      }



      public function generate($what = "js")
      {
            if ($what == "js")
            {
                  $data = "<script type=\"text/javascript\">
                                  var tovalidate = new Array({$this->inputs}\"txtFileName\");
                                  var estimatedtime = 0;
                                  var date1 = \"\";
                                  var milliseconds1 = 0;
                             </script>
                             <script type=\"text/javascript\" src=\"{$this->prefix}include/msep/includes/swfupload/js/swfupload.swfobject.js\"></script>
                             <script type=\"text/javascript\" src=\"{$this->prefix}include/msep/includes/swfupload/js/fileprogress.js\"></script>
                             <script type=\"text/javascript\" src=\"{$this->prefix}include/msep/includes/swfupload/js/handlers.js\"></script>
                             <script type=\"text/javascript\">
                                  var swfu;

                                  window.onload = function ()
                                  {
                                        swfu = new SWFUpload(
                                        {
                                              upload_url: \"{$this->prefix}include/msep/includes/swfupload/upload.php\",

                                              post_params: {
                                                    \"sessid\" : \"".session_id()."\",
                                                    \"plugin\" : \"{$this->core->__('plugin')}\",
                                                    \"path\" : \"{$this->path}\",
                                                    \"id\" : \"{$this->user_id}\"
                                              },

                                              file_post_name: \"the_file\",

                                              file_size_limit : \"{$this->mb} MB\",
                                              file_types : \"{$this->exts}\",
                                              file_types_description : \"All Files\",
                                              file_upload_limit : \"1\",
                                              file_queue_limit : \"1\",

                                              swfupload_loaded_handler : swfUploadLoaded,

                                              file_dialog_start_handler: fileDialogStart,
                                              file_queued_handler : fileQueued,
                                              file_queue_error_handler : fileQueueError,
                                              file_dialog_complete_handler : fileDialogComplete,

                                              upload_progress_handler : uploadProgress,
                                              upload_error_handler : uploadError,
                                              upload_success_handler : uploadSuccess,
                                              upload_complete_handler : uploadComplete,

                                              button_placeholder_id : \"spanButtonPlaceholder\",
                                              button_width: 57,
                                              button_height: 24,
                                              button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
                                              button_cursor: SWFUpload.CURSOR.HAND,

                                              flash_url : \"{$this->prefix}include/msep/includes/swfupload/swf/swfupload.swf\",

                                              custom_settings : {
                                                    progress_target : \"fsUploadProgress\",
                                                    upload_successful : false
                                              },

                                              debug: false
                                        });
                                  };
                             </script>";

                  return $data;
            }
            elseif ($what == "js_multi")
            {
                  $data = "<script type=\"text/javascript\" src=\"{$this->prefix}include/msep/includes/swfupload/js/swfupload.queue_multi.js\"></script>
                            <script type=\"text/javascript\" src=\"{$this->prefix}include/msep/includes/swfupload/js/fileprogress_multi.js\"></script>
                            <script type=\"text/javascript\" src=\"{$this->prefix}include/msep/includes/swfupload/js/handlers_multi.js\"></script>
                            <script type=\"text/javascript\">
                                  var swfu;

                                  window.onload = function() {

                                        var settings = {

                                              flash_url : \"{$this->prefix}include/msep/includes/swfupload/swf/swfupload.swf\",
                                              upload_url: \"{$this->prefix}include/msep/includes/swfupload/upload.php\",

                                              post_params: {
                                                    \"sessid\" : \"".session_id()."\",
                                                    \"plugin\" : \"{$this->core->__('plugin')}\",
                                                    \"path\" : \"{$this->path}\",
                                                    \"album\" : \"{$this->album}\"
                                              },

                                              file_post_name: \"the_file\",

                                              file_size_limit : \"{$this->mb} MB\",
                                              file_types : \"{$this->exts}\",
                                              file_types_description : \"All Files\",
                                              file_upload_limit : 100,
                                              file_queue_limit : 0,

                                              custom_settings : {
                                                    progressTarget : \"fsUploadProgress\",
                                                    uploadButtonId : \"btnUpload\",
                                                    cancelButtonId : \"btnCancel\"
                                              },

                                              debug: false,

                                              button_placeholder_id : \"spanButtonPlaceholder\",
                                              button_width: 57,
                                              button_height: 24,
                                              button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
                                              button_cursor: SWFUpload.CURSOR.HAND,

                                              // The event handler functions are defined in handlers.js
                                              file_queued_handler : fileQueued,
                                              file_queue_error_handler : fileQueueError,
                                              file_dialog_complete_handler : fileDialogComplete,
                                              upload_start_handler : uploadStart,
                                              upload_progress_handler : uploadProgress,
                                              upload_error_handler : uploadError,
                                              upload_success_handler : uploadSuccess,
                                              upload_complete_handler : uploadComplete,
                                              queue_complete_handler : queueComplete	// Queue plugin event
                                      };

                                      swfu = new SWFUpload(settings);
                                };

                           </script>";

                  return $data;
            }
            elseif ($what == "uploader")
            {
                  $data = "<br />

                           <b>*".SE_Language::_get(60000001)."</b><br />

                           <input type=\"text\" size=\"25\" id=\"txtFileName\" readOnly=\"true\" class=\"text\" />

                           <span id=\"spanButtonPlaceholder\"></span>

                           <input id=\"btnUpload\" type=\"button\" value=\"".SE_Language::_get(60000002)."\" class=\"button\" />

                           <br /><br />

                           <div class=\"flash\" style=\"margin-bottom: 2px;\" id=\"fsUploadProgress\"></div>

                           <br />

                           <input type=\"hidden\" name=\"hidFileID\" id=\"hidFileID\" value=\"\" />";

                  return $data;
            }
            elseif ($what == "uploader_multi")
            {
                  $data = "<div class=\"fieldset flash\" id=\"fsUploadProgress\">

                           <span class=\"legend\">".SE_Language::_get(60000015)."</span>

                           </div>

                           <div>

                           <br />

                           <span id=\"spanButtonPlaceholder\"></span>

                           <input id=\"btnUpload\" type=\"button\" value=\"".SE_Language::_get(60000002)."\" class=\"button\" />

                           <input id=\"btnCancel\" type=\"button\" value=\"".SE_Language::_get(60000006)."\" onclick=\"swfu.cancelQueue();\" disabled=\"disabled\" class=\"button\" />

                           </div>";


                  return $data;
            }
      }



      public function upload_dir($user_id = 0)
      {
            $subdir = $user_id+999-(($user_id-1)%1000);
            $dir = "./uploads_{$this->core->__('plugin')}/{$subdir}/{$user_id}/";

            return $dir;
      }


}
?>